package com.enterprise.controller.api.news;

import com.alibaba.fastjson.JSON;
import com.enterprise.controller.api.common.BaseResponse;
import com.enterprise.controller.api.news.vo.NewsResp;
import com.enterprise.entity.News;
import com.enterprise.entity.page.PageModel;
import com.enterprise.service.NewsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;


@Slf4j
@RequestMapping("/api/news")
@Controller("apiNewsController")
public class NewsController {

    @Autowired
    private NewsService newsService;

    @ResponseBody
    @RequestMapping("/queryNewsPage")
    public BaseResponse<PageModel> queryNewsPage(@RequestBody News newsreq){
        log.info("queryNewsPage news: {}" , JSON.toJSONString(newsreq));
        BaseResponse response = new BaseResponse();
        if (newsreq.getOffset() <= 0 || newsreq.getPageSize() <= 0){
            return BaseResponse.fail("分页参数必须大于0");
        }
        PageModel pageModel = newsService.selectPage(newsreq);
        List<News> newsList = pageModel.getList();
        List<NewsResp> newsResps = new ArrayList<>();
        for (News news:newsList) {
            NewsResp newsResp = new NewsResp();
            BeanUtils.copyProperties(news,newsResp);
            newsResp.setNewsimgurl(BaseResponse.httpstr(newsResp.getNewsimgurl()));
            newsResps.add(newsResp);
        }

        pageModel.setList(newsResps);
        log.info("queryNewsPage pageModel: {}" , JSON.toJSONString(pageModel));
        response.setData(pageModel);
        return response;
    }

    @ResponseBody
    @RequestMapping("/queryNewsList")
    public BaseResponse<List<NewsResp>> queryCarList(){
        BaseResponse response = new BaseResponse();
        List<News> newsList = newsService.selectList(new News());
        List<NewsResp> newsResps = new ArrayList<>();
        for (News news:newsList) {
            NewsResp newsResp = new NewsResp();
            BeanUtils.copyProperties(news,newsResp);
            newsResp.setNewsimgurl(BaseResponse.httpstr(newsResp.getNewsimgurl()));
            newsResps.add(newsResp);
        }
        log.info("queryNewsList newsResps: {}" , JSON.toJSONString(newsResps));
        response.setData(newsResps);
        return response;
    }


    @ResponseBody
    @RequestMapping("/queryNewsView/{id}")
    public BaseResponse<NewsResp> queryNewsView(@PathVariable("id") String id){
        BaseResponse response = new BaseResponse();
        News news = newsService.selectById(Integer.valueOf(id));
        NewsResp newsResp = new NewsResp();
        BeanUtils.copyProperties(news,newsResp);
        newsResp.setNewsimgurl(BaseResponse.httpstr(newsResp.getNewsimgurl()));
        log.info("queryNewsView newsResp: {}" , JSON.toJSONString(newsResp));
        response.setData(newsResp);
        return response;
    }


}
